﻿function Product(id, description) {
    this.getId = function () {
        return id;
    };
    this.getDescription = function () {
        return description;
    };
}

function Cart() {
    var items = [];
    this.addItem = function (item) {
        items.push(item);
    };
}

(function () {
    var products = [new Product(1, "p001"), new Product(2, "P002"), new Product(3, "P003")],
          cart = new Cart();

    function addToCart() {
        var productId = $(this).attr("id");
        var product = $.grep(products, function (x) {
            return x.getId() == productId;
        })[0];
        cart.addItem(product);

        var newItem = $('<li></li>').html(product.getDescription()).attr('id', product.getId())
                                                               .appendTo("#cart");
    }

    products.forEach(function (product) {
        var newItem = $('<li></li>').html(product.getDescription()).attr('id', product.getId())
                                                                .dblclick(addToCart).appendTo("#products");
    });
})();